//
//  CustomDatePickerView.m
//  CustomDateTimePicker
//
//  Created by lanou3g on 15/7/17.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CustomDatePickerView.h"
#import "HomePageViewController.h"

#define RGBA(r,g,b,a)               [UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:a]

@interface CustomDatePickerView()

@end

@implementation CustomDatePickerView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self setTimeBroadcastView];
    }
    return self;
}
#pragma mark -custompicker
//设置自定义datepicker界面
- (void)setTimeBroadcastView{
    
    self.setDateRemind = [[UILabel alloc]initWithFrame:CGRectMake(20, 115, self.bounds.size.width - 40, 18)];
    _setDateRemind.text = @"请设置预产期";
    [_setDateRemind setFont:[UIFont systemFontOfSize:20]];
    [_setDateRemind setTextColor:RGBA(255, 100, 51, 1)];
    [_setDateRemind setTextAlignment:NSTextAlignmentCenter];
    [self addSubview:_setDateRemind];
    //当前picker显示的时间
    self.nowPickerShowTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 350,self.bounds.size.width - 40 , 18)];
    [_nowPickerShowTimeLabel setFont:[UIFont systemFontOfSize:18.0]];
    [_nowPickerShowTimeLabel setTextColor:RGBA(51, 51, 51, 1)];
    [_nowPickerShowTimeLabel setTextAlignment:NSTextAlignmentCenter];
    NSDate *now = [NSDate date];
    //设置日期格式
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMdd"];
    //根据格式转化为string
    NSString *dateString = [dateFormatter stringFromDate:now];
    //获取年
    NSString *yearStr = [dateString substringWithRange:NSMakeRange(0, 4)];
    //获取月
    NSInteger monthInt = [dateString substringWithRange:NSMakeRange(4, 2)].integerValue;
    //获取day
    NSInteger dayInt = [dateString substringWithRange:NSMakeRange(6, 2)].integerValue;
    
    self.nowPickerShowTimeLabel.text = [NSString stringWithFormat:@"%@ 年  %ld 月  %ld 日",yearStr , (long)monthInt,(long)dayInt];
    [self addSubview:_nowPickerShowTimeLabel];
    
    //定时播放显示视图
    self.timeBroadcastView = [[UIView alloc] initWithFrame:CGRectMake(50, 144, 275, 190.0)];
    _timeBroadcastView.layer.cornerRadius = 8;//设置视图圆角
    _timeBroadcastView.layer.masksToBounds = YES;//告诉layer将位于它之下的layer都遮盖住
    CGColorRef cgColor = [UIColor colorWithRed:221.0/255.0 green:221.0/255.0 blue:221.0/255.0 alpha:1.0].CGColor;
    _timeBroadcastView.layer.borderColor = cgColor;
    _timeBroadcastView.layer.borderWidth = 2.0;
    [self addSubview:_timeBroadcastView];
    UIView *beforeSepLine = [[UIView alloc] initWithFrame:CGRectMake(0, 39, 278.5, 1.5)];
    [beforeSepLine setBackgroundColor:RGBA(237.0, 237.0, 237.0, 1.0)];
    [_timeBroadcastView addSubview:beforeSepLine];
    UIView *middleSepView = [[UIView alloc] initWithFrame:CGRectMake(0, 75, 278.5, 38)];
    [middleSepView setBackgroundColor:RGBA(249.0, 138.0, 20.0, 1.0)];
    [_timeBroadcastView addSubview:middleSepView];
    UIView *bottomSepLine = [[UIView alloc] initWithFrame:CGRectMake(0, 150.5, 278.5, 1.5)];
    [bottomSepLine setBackgroundColor:RGBA(237.0, 237.0, 237.0, 1.0)];
    [_timeBroadcastView addSubview:bottomSepLine];
    
    [self setYearScrollView];
    [self setMonthScrollView];
    [self setDayScrollView];
    
    //显示所选时间是否合法
    self.selectTimeIsNotLegalLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 373, 335, 15)];
    [_selectTimeIsNotLegalLabel setBackgroundColor:[UIColor clearColor]];
    [_selectTimeIsNotLegalLabel setFont:[UIFont systemFontOfSize:15.0]];
    [_selectTimeIsNotLegalLabel setTextAlignment:NSTextAlignmentCenter];
    [self addSubview:_selectTimeIsNotLegalLabel];
    
    self.OkBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _OkBtn.frame = CGRectMake(100, 420, 175, 40);
    _OkBtn.titleLabel.font = [UIFont systemFontOfSize:20];
    [_OkBtn setTitle:@"奔跑吧 宝宝" forState:UIControlStateNormal];
    [_OkBtn addTarget:self action:@selector(goHomePage:) forControlEvents:UIControlEventTouchUpInside];
    [_OkBtn setBackgroundColor:[UIColor orangeColor]];
    _OkBtn.layer.cornerRadius = 4;
    [self addSubview:_OkBtn];

    
}

//设置button点击事件
-(void)goHomePage:(UIButton *)button{

    [[self viewController] dismissViewControllerAnimated:YES completion:nil];
    
//    [button.superview.superview removeFromSuperview];
//    //NSUserDefaults 是系统提供的一个操作本地文件的类,他可以对本地文件进行写入和读取数据;
//    //创建对象的方式单例模式,既在整个应用程序中,用单例创建出来的对象只有一个
    [[NSUserDefaults standardUserDefaults] setObject:_selectDate forKey:@"selectData"];

}

- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}



//设置年的滚动视图
- (void)setYearScrollView
{
    //年份滚动视图
    _yearScrollView = [[MXSCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, 100.0, 190.0)];
    NSInteger yearint = [self setNowTimeShow:0];
    [_yearScrollView setCurrentSelectPage:(yearint-2002)];
    _yearScrollView.delegate = self;
    _yearScrollView.datasource = self;
    [self setAfterScrollShowView:_yearScrollView andCurrentPage:1];
    [_timeBroadcastView addSubview:_yearScrollView];
}
//设置月时分的滚动视图
- (void)setMonthScrollView
{
    _monthScrollView = [[MXSCycleScrollView alloc] initWithFrame:CGRectMake(100.0, 0, 87.5, 190.0)];
    NSInteger monthint = [self setNowTimeShow:1];
    [_monthScrollView setCurrentSelectPage:(monthint-3)];
    _monthScrollView.delegate = self;
    _monthScrollView.datasource = self;
    [self setAfterScrollShowView:_monthScrollView andCurrentPage:1];
    [_timeBroadcastView addSubview:_monthScrollView];
}
//设置日的滚动视图
- (void)setDayScrollView
{
    _dayScrollView = [[MXSCycleScrollView alloc] initWithFrame:CGRectMake(187.5, 0, 87.5, 190.0)];
    NSInteger dayint = [self setNowTimeShow:2];
    [_dayScrollView setCurrentSelectPage:(dayint-3)];
    _dayScrollView.delegate = self;
    _dayScrollView.datasource = self;
    [self setAfterScrollShowView:_dayScrollView andCurrentPage:1];
    [_timeBroadcastView addSubview:_dayScrollView];
}


- (void)setAfterScrollShowView:(MXSCycleScrollView*)scrollview  andCurrentPage:(NSInteger)pageNumber
{
    UILabel *oneLabel = [[(UILabel*)[[scrollview subviews] objectAtIndex:0] subviews] objectAtIndex:pageNumber];
    [oneLabel setFont:[UIFont systemFontOfSize:14]];
    [oneLabel setTextColor:RGBA(186.0, 186.0, 186.0, 1.0)];
    UILabel *twoLabel = [[(UILabel*)[[scrollview subviews] objectAtIndex:0] subviews] objectAtIndex:pageNumber+1];
    [twoLabel setFont:[UIFont systemFontOfSize:16]];
    [twoLabel setTextColor:RGBA(113.0, 113.0, 113.0, 1.0)];
    
    UILabel *currentLabel = [[(UILabel*)[[scrollview subviews] objectAtIndex:0] subviews] objectAtIndex:pageNumber+2];
    [currentLabel setFont:[UIFont systemFontOfSize:18]];
    [currentLabel setTextColor:[UIColor whiteColor]];
    
    UILabel *threeLabel = [[(UILabel*)[[scrollview subviews] objectAtIndex:0] subviews] objectAtIndex:pageNumber+3];
    [threeLabel setFont:[UIFont systemFontOfSize:16]];
    [threeLabel setTextColor:RGBA(113.0, 113.0, 113.0, 1.0)];
    UILabel *fourLabel = [[(UILabel*)[[scrollview subviews] objectAtIndex:0] subviews] objectAtIndex:pageNumber+4];
    [fourLabel setFont:[UIFont systemFontOfSize:14]];
    [fourLabel setTextColor:RGBA(186.0, 186.0, 186.0, 1.0)];
}

#pragma mark mxccyclescrollview delegate
#pragma mark mxccyclescrollview databasesource
- (NSInteger)numberOfPages:(MXSCycleScrollView*)scrollView
{
    if (scrollView == _yearScrollView) {
        return 99;
    }
    else if (scrollView == _monthScrollView)
    {
        return 12;
    }
    else if (scrollView == _dayScrollView)
    {
        return 31;
    }
    return 0;
}

- (UIView *)pageAtIndex:(NSInteger)index andScrollView:(MXSCycleScrollView *)scrollView
{
    UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, scrollView.bounds.size.width, scrollView.bounds.size.height/5)];
    l.tag = index+1;
    if (scrollView == _yearScrollView) {
        l.text = [NSString stringWithFormat:@"%ld年",2000+index];
    }
    else if (scrollView == _monthScrollView)
    {
        l.text = [NSString stringWithFormat:@"%ld月",1+index];
    }
    else if (scrollView == _dayScrollView)
    {
        l.text = [NSString stringWithFormat:@"%ld日",1+index];
    }
    
    l.font = [UIFont systemFontOfSize:12];
    l.textAlignment = NSTextAlignmentCenter;
    l.backgroundColor = [UIColor clearColor];
    return l;
}
//设置现在时间
- (NSInteger)setNowTimeShow:(NSInteger)timeType
{
    NSDate *now = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *dateString = [dateFormatter stringFromDate:now];
    switch (timeType) {
        case 0:
        {
            NSRange range = NSMakeRange(0, 4);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString.integerValue;
        }
            break;
        case 1:
        {
            NSRange range = NSMakeRange(4, 2);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString.integerValue;
        }
            break;
        case 2:
        {
            NSRange range = NSMakeRange(6, 2);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString.integerValue;
        }
            break;
        case 3:
        {
            NSRange range = NSMakeRange(8, 2);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString.integerValue;
        }
            break;
        case 4:
        {
            NSRange range = NSMakeRange(10, 2);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString.integerValue;
        }
            break;
        case 5:
        {
            NSRange range = NSMakeRange(12, 2);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString.integerValue;
        }
            break;
        default:
            break;
    }
    return 0;
}
//选择设置的播报时间
- (void)selectSetBroadcastTime
{
    UILabel *yearLabel = [[(UILabel*)[[_yearScrollView subviews] objectAtIndex:0] subviews] objectAtIndex:3];
    UILabel *monthLabel = [[(UILabel*)[[_monthScrollView subviews] objectAtIndex:0] subviews] objectAtIndex:3];
    UILabel *dayLabel = [[(UILabel*)[[_dayScrollView subviews] objectAtIndex:0] subviews] objectAtIndex:3];
    
    NSInteger yearInt = yearLabel.tag + 1999;
    NSInteger monthInt = monthLabel.tag;
    NSInteger dayInt = dayLabel.tag;
    NSString *taskDateString = [NSString stringWithFormat:@"%ld%02ld%02ld",(long)yearInt,(long)monthInt,(long)dayInt];
    NSLog(@"Now----%@",taskDateString);
}
//滚动时上下标签显示(当前时间和是否为有效时间)
- (void)scrollviewDidChangeNumber
{
    UILabel *yearLabel = [[(UILabel*)[[_yearScrollView subviews] objectAtIndex:0] subviews] objectAtIndex:3];
    UILabel *monthLabel = [[(UILabel*)[[_monthScrollView subviews] objectAtIndex:0] subviews] objectAtIndex:3];
    UILabel *dayLabel = [[(UILabel*)[[_dayScrollView subviews] objectAtIndex:0] subviews] objectAtIndex:3];
    
    
    NSInteger yearInt = yearLabel.tag + 1999;
    NSInteger monthInt = monthLabel.tag;
    NSInteger dayInt = dayLabel.tag;
    _nowPickerShowTimeLabel.text = [NSString stringWithFormat:@"%ld年%ld月%ld日",(long)yearInt,(long)monthInt,(long)dayInt];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *selectTimeString = [NSString stringWithFormat:@"%ld-%02ld-%02ld",(long)yearInt,(long)monthInt,(long)dayInt];
    _selectDate = [dateFormatter dateFromString:selectTimeString];
    NSDate *nowDate = [NSDate date];
    NSString *nowString = [dateFormatter stringFromDate:nowDate];
    NSDate *nowStrDate = [dateFormatter dateFromString:nowString];
    
    //计算当前时间和预产期的天数差
    long dd = [_selectDate timeIntervalSince1970] - [nowDate timeIntervalSince1970];
    _daysDiffer = (dd + 86400)/ 86400 + 1;
    NSLog(@"%ld",(long)_daysDiffer);
    if ((NSOrderedAscending == [_selectDate compare:nowStrDate]) || _daysDiffer > 280) {//选择的时间与当前系统时间做比较
        [_selectTimeIsNotLegalLabel setTextColor:RGBA(155, 155, 155, 1)];
        _selectTimeIsNotLegalLabel.text = @"温馨提示：预产期设置不合理，无法提交";
        [_OkBtn setEnabled:NO];
    }
    else
    {
        _selectTimeIsNotLegalLabel.text = @"";
        [_OkBtn setEnabled:YES];
    }
}



@end





























